package app.ui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import app.action.AppInit;
import app.action.Utilities;
import app.textSrc.TextSource;

/**
 * @author mvv
 * 
 */
public class MainFrame extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8924308466844158184L;

	private final GridBagConstraints constraints = new GridBagConstraints();
	public static final String LASTDIR = "lastDir";
	private AppInit appInit;
	private TextSource textSource;
	private Properties appProp;
	private QuestionUI questionUI = null;
	private StartUi startUi = null;
	private File dataFileDir = null;

	public MainFrame(AppInit appInit) {
		super("TEST");

		this.appInit = appInit;
		this.appProp = appInit.getUtilities().getAppProp();
		this.startUi = new StartUi(this);

		this.constraints.fill = GridBagConstraints.BOTH;
		this.constraints.weightx = 1;
		this.constraints.weighty = 1;

		initialize();
	}

	public void initialize() {
		int x, y, width, height;
		x = Integer.parseInt(appProp.getProperty("ScrLocX"));
		y = Integer.parseInt(appProp.getProperty("ScrLocY"));
		width = Integer.parseInt(appProp.getProperty("ScrWid"));
		height = Integer.parseInt(appProp.getProperty("ScrHei"));

		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setBounds(x, y, width, height);
		setBackground(Color.BLACK);
		setMinimumSize(new Dimension(width, height));
		setLayout(new GridBagLayout());
		if (appProp.getProperty("appMode").equalsIgnoreCase("PROD")) {
			setExtendedState(MAXIMIZED_BOTH);
		}

		startUi.addThisToMainFrame();

		MainFrame_Resize comLis = new MainFrame_Resize();
		addComponentListener(comLis);
	}

	private void addQuestionUI() {
		// BEGIN
		if (textSource != null) {
			if (questionUI != null) {
				this.getContentPane().remove(questionUI);
				this.questionUI = null;
			}
			questionUI = new QuestionUI(this, false);
		} else {
			return;
		}
		// END
		questionUI.addThisToMainFrame();
	}

	private void settingStartUi(String path) {
		JTextField jField = startUi.getjTextField1();

		jField.setText(path);
		jField.setEditable(false);
		jField.setCaretPosition(1);
	}

	public void setCurrentDirectory(File f) {
		appProp.setProperty(LASTDIR, f.getParentFile().getAbsolutePath());
		saveCurrentProperties();
	}

	public void saveCurrentProperties() {
		String curDir = System.getProperty("user.dir")
				+ "/application.properties";
		OutputStream out;
		try {
			out = new FileOutputStream(new File(curDir));
			appProp.store(out, "Author: fatm_1990@yahoo.com");
			out.close();
		} catch (FileNotFoundException e) {
			System.err.println("Create output file failed. -- "
					+ e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public AppInit getAppInit() {
		return appInit;
	}

	public void setAppInit(AppInit appInit) {
		this.appInit = appInit;
	}

	public TextSource getTextSource() {
		return textSource;
	}

	public void setTextSource(File file) {
		if (file != null) {
			TextSource textSource = null;

			Utilities utilities = appInit.getUtilities();
			utilities.setQueProp(utilities.readProperties(file));
			try {
				textSource = new TextSource(utilities.getQueProp());
			} catch (IOException e) {
				JOptionPane.showMessageDialog(this, e.getMessage());
				e.printStackTrace();
				return;
			}

			settingStartUi(file.getPath());
			this.dataFileDir = file.getParentFile();
			this.textSource = textSource;
			addQuestionUI();
			// -- set current Directory in property file
			setCurrentDirectory(file);
			// -- set main Frame title
			String frmName = file.getName();
			setTitle(frmName);
		}
	}

	public Properties getAppProp() {
		return appProp;
	}

	public QuestionUI getQuestionUI() {
		return questionUI;
	}

	public StartUi getStartUi() {
		return startUi;
	}

	public File getDataFileDir() {
		return dataFileDir;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @ All extension of JPanel is use this add to
	 * 
	 * @ stretch full out area of main Frame
	 * 
	 * @ This method will disable automatically the StartUi
	 */
	@Override
	public Component add(Component comp) {
		if (!(comp instanceof StartUi)) {
			startUi.setVisible(false);
		}

		super.getContentPane().add(comp, constraints);
		return comp;
	}
}

class MainFrame_Resize extends ComponentAdapter {

	public void componentResized(ComponentEvent e) {
		MainFrame mFrame = (MainFrame) e.getSource();

		Dimension new_D = mFrame.getSize();
		Dimension old_D = mFrame.getMinimumSize();

		if (new_D.width < old_D.width || new_D.height < old_D.height) {
			mFrame.setSize(old_D);
		}
	}
}
